package cn.easybuy.web.pre;

import cn.easybuy.biz.UserBiz;
import cn.easybuy.bizImpl.UserBizImpl;
import cn.easybuy.entity.User;
import cn.easybuy.utils.EmptyUtils;
import cn.easybuy.web.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/user")
public class UserServlet extends BaseServlet {
    UserBiz userBiz=new UserBizImpl();
    public String regist(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        List<User> users=userBiz.listAll();
        String loginname=request.getParameter("LoginName");
        if (EmptyUtils.isEmpty(loginname)){
            out.print("<script>alert('登录名不能为空！');");
            return "Regist.jsp";
        }
        for(User user:users){
            if(user.getLoginName().equals(loginname)){
                out.print("<script>alert('该用户已存在！');");
                return "Regist.jsp";
            }
        }
        String userName=request.getParameter("userName");
        if (EmptyUtils.isEmpty(userName)){
            out.print("<script>alert('用户名不能为空！');");
            return "Regist.jsp";
        }
        String password=request.getParameter("pwd");
        if (EmptyUtils.isEmpty(password)){
            out.print("<script>alert('密码不能为空！');");
            return "Regist.jsp";
        }
        String email=request.getParameter("email");
        if (EmptyUtils.isEmpty(email)){
            out.print("<script>alert('邮箱不能为空！');");
            return "Regist.jsp";
        }
        String mobile=request.getParameter("mobile");
        if (EmptyUtils.isEmpty(mobile)){
            out.print("<script>alert('手机号不能为空！');");
            return "Regist.jsp";
        }
        String identityCode=request.getParameter("identityCode");
        if (EmptyUtils.isEmpty(identityCode)){
            out.print("<script>alert('身份证号码不能为空！');");
            return "Regist.jsp";
        }
        Integer sex=Integer.parseInt(request.getParameter("sex"));

        return "";
    }

}
